package sz.szsmk.citizencard.ui.activity;

import android.content.Intent;
import android.net.Uri;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ServiceDetailActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.CollecQueryRes;
import sz.szsmk.citizencard.model.network.bean.response.ShopListRes;
import sz.szsmk.citizencard.model.network.bean.response.WebsiteInfoRes;
import sz.szsmk.citizencard.mvp.contract.IServiceDetailContact;
import sz.szsmk.citizencard.mvp.presenter.ServiceDetailPresenter;
import sz.szsmk.citizencard.ui.adapter.ServiceDetailAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.ShowImageUtils;
import sz.szsmk.citizencard.util.Utils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/12 18:10
 * description:
 */
@Route("netdetail")
public class ServiceDetailActivity extends IBaseActivity<ServiceDetailPresenter, ServiceDetailActivityBinding> implements
        IServiceDetailContact.IServiceDetailView, View.OnClickListener {

    private int id;
    private double longitude;
    private double latitude;
    private ServiceDetailAdapter serviceDetailAdapter;
    private ArrayList<String> mDetail = new ArrayList<>();
    private ArrayList<Integer> ints = new ArrayList<>();

    private String telephone;

    @Override
    protected int attachLayoutRes() {
        return R.layout.service_detail_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        RecyclerView detailRv = dataBinding.detailRv;
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        mPresenter.queryCollect();
        mPresenter.websiteInfo("" + longitude, "" + latitude, "" + id);
        longitude = intent.getDoubleExtra("longitude", 0);
        latitude = intent.getDoubleExtra("latitude", 0);
        dataBinding.titleLayout.back.setOnClickListener(this);
        dataBinding.titleLayout.settingTitle.setText("网点详情");
        dataBinding.netDetailAdress.setOnClickListener(this);
        String type = intent.getStringExtra("type");
        if (type != null && type.equals("coll")) {
            dataBinding.netDetailBar.setChecked(true);
        }
        dataBinding.netDetailTele.setOnClickListener(this);
        dataBinding.netDetailBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String access_token = ACache.get(ServiceDetailActivity.this).getAsString("access_token");
                boolean checked = dataBinding.netDetailBar.isChecked();
                if (checked) {
                    mPresenter.addCollection(id, access_token);
                } else {
                    mPresenter.cancelCollec(id, access_token);
                }
            }
        });
        GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 2);
        detailRv.setLayoutManager(linearLayoutManager);
        serviceDetailAdapter = new ServiceDetailAdapter(this);
        detailRv.setAdapter(serviceDetailAdapter);
    }

    @Override
    protected void updateViews() {


    }

    @Override
    public void onClick(View v) {
        int vId = v.getId();
        if (vId == R.id.back) {
            finish();
        } else if (vId == R.id.net_detail_tele) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + telephone);
            intent.setData(data);
            startActivity(intent);
        } else if (vId == R.id.net_detail_adress) {
            ShopListRes.ShopListInfosBean infosBean = new ShopListRes.ShopListInfosBean();
            infosBean.setShopName(dataBinding.serviceDetailShopname.getText().toString());
            infosBean.setLatitude(latitude);
            infosBean.setLongitude(longitude);
            Utils.startMap(ServiceDetailActivity.this, infosBean);
        }
    }

    @Override
    public void websiteInfoSucc(WebsiteInfoRes response) {
        if (response != null) {
            setDetailData(response);
        }
        String service_details = response.getService_details();
        if (service_details != null) {
//            String serviceDetails = "市民卡售后,售卡,现金充值,充值卡充值,企服卡,园林年卡,惠民休闲年卡,月票高龄卡,爱心卡,公共自行车,漫游卡,";
            String[] ary = service_details.split(",");
            for (String item : ary) {
                mDetail.add(item);
            }
            for (int i = 0; i < ary.length; i++) {

            }
            serviceDetailAdapter.setItemData(mDetail);
            serviceDetailAdapter.notifyDataSetChanged();
        }

    }

    private void setDetailData(WebsiteInfoRes response) {
        telephone = response.getTelephone();
        if (ints != null) {
            if (ints.contains(response.getId())) {
                dataBinding.netDetailBar.setChecked(true);
                L.e(Constant.TAG, "-----------------------------------");
            }
        }
        dataBinding.netDetailAdress.setText(response.getAddress());
        String telephone = response.getTelephone();
        if (telephone == null || telephone.equals("")) {
            dataBinding.netDetailTele.setVisibility(View.GONE);
            dataBinding.netDetailTeliv.setVisibility(View.GONE);
        } else {
            dataBinding.netDetailTele.setVisibility(View.VISIBLE);
            dataBinding.netDetailTeliv.setVisibility(View.VISIBLE);
            dataBinding.netDetailTele.setText("联系商家");
        }
        dataBinding.detailTime.setText(response.getBusiness_hours());
//        dataBinding.netDetailTele.setText(response.getTelephone());
        dataBinding.serviceDetailShopname.setText(response.getShopName());
        int id = response.getId();
        L.e(Constant.TAG, "服务网点ID----->" + id);
        String picurl = response.getPicUrl();
        ShowImageUtils.showImageViewToCircle(DaggerApplication.getIns(), R.drawable.zhanweitu, picurl, dataBinding.imageView);

        boolean b = useList(ints, response.getId() + "");
        L.e(Constant.TAG, "-----" + b);


    }

    public static boolean useList(ArrayList<Integer> arr, String value) {
        return Arrays.asList(arr).contains(value);
    }

    @Override
    public void websiteInfoFail() {

    }

    @Override
    public void addSuccess(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        dataBinding.netDetailBar.setChecked(true);

    }

    @Override
    public void addFail(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void canSuccess(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        dataBinding.netDetailBar.setChecked(false);
    }

    @Override
    public void canFail(String msg) {

    }

    @Override
    public void queryCollectSucc(List<CollecQueryRes.DataBean> data) {
        if (data != null) {
            ints = new ArrayList<>();
            ints.clear();
            for (int i = 0; i < data.size(); i++) {
                CollecQueryRes.DataBean dataBean = data.get(i);
                int areaId = dataBean.getServiceOfficeId();
                ints.add(areaId);
            }
        }

        String json = new Gson().toJson(ints);
        if (json.contains(id + "")) {
            dataBinding.netDetailBar.setChecked(true);
//            Toast.makeText(this, "已收藏", Toast.LENGTH_SHORT).show();
        } else {
            dataBinding.netDetailBar.setChecked(false);
        }
    }

    @Override
    public void queryCollectFail() {

    }
}
